package cn.nawang.ebeim.api.repository.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName: TaskFilesinfoLogRepositoryImpl
 * @Description: 任务文件传输操作资源实现
 * @author fanyb
 * @date Aug 7, 2015 5:52:14 PM
 * @version v1.0
 */
public class TaskFilesinfoLogRepositoryImpl {
  @Autowired
  private EntityManagerFactory entityManagerFactory;

  /**
   * 
   * @author fanyb
   * @Description: 获取终端已用空间（b）
   * @param terminalId 终端编号
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @return double 终端已用空间（b）
   * @date Aug 11, 201510:00:54 AM
   *
   * @修改人
   * @修改日期
   */
  public double getTerminalSpaceUse(long startTime, long endTime, String terminalId) {
    String sql =
        "select sum(space_size) from tb_task_filesinfo_log where terminal_id='" + terminalId + "'";
    if (startTime != 0 && endTime != 0) {
      sql += " and sg_time<=" + endTime + " and sg_time>=" + startTime;
    }
    List objList = null;
    double spaceSize = 0.0;
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
      Query query = em.createNativeQuery(sql);
      objList = query.getResultList();
      if (objList.get(0) != null) {
        spaceSize = Double.valueOf(objList.get(0).toString());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }finally{
      em.close();
    }
    return spaceSize;
  }
}
